package cn.aj.system.controller;

import cn.aj.system.service.IMenuService;
import cn.aj.system.domain.Menu;
import cn.aj.system.query.MenuQuery;
import cn.aj.basic.utils.AjaxResultUtil;
import cn.aj.basic.utils.PageListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResultUtil addOrUpdate(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.add(menu);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.getAjaxResult().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResultUtil delete(@PathVariable("id") Long id){
        try {
            menuService.delete(id);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResultUtil.getAjaxResult().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Menu get(@PathVariable("id")Long id)
    {
        return menuService.getById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Menu> list(){

        return menuService.loadAll();
    }

    /**
    * @Title: list
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/29 0029 21:44
    * @Parameters: []
    * @Return java.util.List<cn.aj.system.domain.Menu>
     *     父级菜单
    */
    @GetMapping("/first")
    public List<Menu> first(){
        return menuService.first();
    }

    /**
    * @Title: tree
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/30 0030 16:39
    * @Parameters: []
    * @Return java.util.List<cn.aj.system.domain.Menu>
     *     角色展示无限极树
    */
    @GetMapping("/tree")
    public List<Menu> tree(){
        return menuService.tree();
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageListUtil<Menu> json(@RequestBody MenuQuery query)
    {
        return menuService.loadPage(query);
    }

    /**
    * @Title: batchDelete
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/29 0029 17:26
    * @Parameters: [id]
    * @Return cn.aj.basic.utils.AjaxResultUtil
     * 批量删除
    */
    @PatchMapping
    public AjaxResultUtil batchDelete(@RequestBody List<Long> ids){
        try {
            menuService.batchDelete(ids);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.getAjaxResult().fail();
        }
    }


}
